import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../layout/index.vue'
import { useRoleStore } from '../stores/role'

// 固定路由表
const staticRoutes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    ...staticRoutes,
    {
      path: '/',
      name: 'home',
      component: Layout,
      //redirect: '/user', // 默认重定向到用户页面
      children: [
        {
          path: 'user',
          name: 'user',
          component: () => import('../views/user/index.vue'),
        },
        {
          path: 'student',
          name: 'student',
          component: () => import('../views/student/index.vue'),
        },
        {
          path: 'admin',
          name: 'admin',
          component: () => import('../views/admin/index.vue'),
          meta: { requiredRole: 'admin' },
        },
        {
          path: 'company',
          name: 'company',
          component: () => import('../views/company/index.vue'),
          meta: { requiredRole: 'company' },
        },
      ],
    },
    // 管理员专用路由
    {
      path: '/admin',
      name: 'adminPanel',
      component: Layout,
      redirect: '/admin/test',
      children: [
        // 管理员页面
        {
          path: 'test',
          name: 'test',
          component: () => import('../views/admin/test.vue'),
          meta: { requiredRole: 'admin' }, // 只有管理员可以访问测试页面
        },
        {
          path: 'user-management',
          name: 'userManagement',
          component: () => import('../views/admin/UserTable.vue'),
          meta: { requiredRole: 'admin' },
        },
        {
          path: 'com-management',
          name: 'comManagement',
          component: () => import('../views/admin/ComTable.vue'),
          meta: { requiredRole: 'admin' },
        },
        {
          path: 'emp-management',
          name: 'empManagement',
          component: () => import('../views/admin/EmpTable.vue'),
          meta: { requiredRole: 'admin' },
        },
      ],
    },
    //企业路由
    {
      path: '/company',
      name: 'companyhome',
      component: Layout,
      children: [
        // 管理员页面
        // {
        //   path: 'test',
        //   name: 'test',
        //   component: () => import('../views/company/index.vue'),
        //   meta: { requiredRole: 'admin' }, // 只有管理员可以访问测试页面
        // },
        {
          path: 'comidv',
          name: 'comidv',
          component: () => import('../views/company/idv.vue'),
          meta: { requiredRole: 'company' },
          //meta: { requiredRole: 'admin' },
        },
        {
          path: 'comemp',
          name: 'comemp',
          component: () => import('../views/company/EmpTable.vue'),
          meta: { requiredRole: 'company' },
          //meta: { requiredRole: 'admin' },
        },
      ],
    },
  ],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const roleStore = useRoleStore()

  // 检查路由是否需要特定角色权限
  if (to.meta.requiredRole) {
    const requiredRole = to.meta.requiredRole as string

    // 检查权限
    if (roleStore.hasPermission(requiredRole)) {
      next()
    } else {
      // 权限不足，重定向到用户页面
      next('/user')
    }
  } else {
    // 不需要特定权限的路由
    next()
  }
})

export default router
